﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Globalization;
using System.Web.UI.WebControls.WebParts;

namespace FlashSolution
{
    public class EditorPartCollapsableTable : Table
    {

        public HtmlGenericControl control;

        public EditorPartCollapsableTable(EditorPart toolPart, string expandToolTip, string collapseToolTip, string displayText, string id, string helpurl)
            : base()
        {
            this.CellSpacing = 0;
            this.CellPadding = 0;
            this.Style.Add(HtmlTextWriterStyle.Width, "100%");
            toolPart.Controls.Add(this);

            control = new HtmlGenericControl("div");
            control.ID = id;
            toolPart.Controls.Add(control);

            TableRow row = new TableRow();
            this.Controls.Add(row);
            TableCell cell = new TableCell();
            row.Controls.Add(cell);

            
            HyperLink link = new HyperLink();
            link.ID = id + "_IMAGEANCHOR";
            link.Attributes.Add("onkeydown", "javascript:MSOMenu_KeyboardClick(this, 13, 32);");
            link.Attributes.Add("TabIndex", "0");
            link.Style.Add(HtmlTextWriterStyle.Cursor, "hand");

            Image image = new Image();
            image.ID = id + "_IMAGE";
            
            HyperLink link2 = new HyperLink();
            link2.Attributes.Add("onkeydown", "MSOMenu_KeyboardClick(this, 13, 32);");
            link2.Attributes.Add("TabIndex", "-1");
            link2.ID = id + "_ANCHOR";
            link2.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
            link2.Text = " &nbsp;" + SPHttpUtility.HtmlEncode(displayText);
            
            Table table2 = new Table();
            TableRow row2 = new TableRow();
            TableCell cell2 = new TableCell();
            TableCell cell3 = new TableCell();
            table2.Width = Unit.Percentage(100.0);
            table2.CellSpacing = 0;
            table2.CellPadding = 0;
            cell.Controls.Add(table2);
            table2.Controls.Add(row2);
            row2.Controls.Add(cell2);
            row2.Controls.Add(cell3);

            HtmlGenericControl control2 = new HtmlGenericControl("div");
            control2.Attributes.Add("class", "UserSectionTitle");
            cell2.Controls.Add(control2);
            control2.Controls.Add(link);
            link.Controls.Add(new LiteralControl("&nbsp;"));
            link.Controls.Add(image);
            link.Controls.Add(new LiteralControl("&nbsp;"));
            control2.Controls.Add(link2);
            if (!string.IsNullOrEmpty(helpurl))
            {
                HtmlGenericControl control3 = new HtmlGenericControl("div");
                control3.Attributes.Add("class", "UserSectionTitle");
                cell3.Controls.Add(control3);
                //if (string.Compare(SPResource.GetString("AlignRight", new object[0]), "Right", StringComparison.OrdinalIgnoreCase) != 0)
                {
                    cell3.HorizontalAlign = HorizontalAlign.Left;
                }
                /*else
                {
                    cell3.HorizontalAlign = HorizontalAlign.Right;
                }*/
                HyperLink link3 = new HyperLink();
                link3.NavigateUrl = "javascript:var wndHelp=window.open('" + helpurl + "', \"STSHELP\",\"height=500,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,width=475\");wndHelp.focus();";//HelpWindowKey
                link3.Text = "Help";// SPHttpUtility.NoEncode(Resources.GetString("MultiPagesHelpLinkText"));
                control3.Controls.Add(link3);
            }
            link.Attributes.Add("name", link.ClientID);
            image.Attributes.Add("name", image.ClientID);
            string script = string.Format(CultureInfo.InvariantCulture, "function mso_onclick{0}() {{ MSOTlPn_ToggleDisplay('{1}', '{2}', '{3}', '{4}', '{5}', '{6}'); }}", new object[] { id, SPHttpUtility.EcmaScriptStringLiteralEncode(control.ClientID), SPHttpUtility.EcmaScriptStringLiteralEncode(image.ClientID), SPHttpUtility.EcmaScriptStringLiteralEncode(link2.ClientID), SPHttpUtility.EcmaScriptStringLiteralEncode(expandToolTip), SPHttpUtility.EcmaScriptStringLiteralEncode(collapseToolTip), SPHttpUtility.EcmaScriptStringLiteralEncode(link.ClientID) });
            toolPart.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "mso_onclick" + id, script, true);
            link.Attributes.Add("onclick", "javascript:mso_onclick" + id + "();");
            link2.Attributes.Add("onclick", "javascript:mso_onclick" + id + "();");
            link2.Attributes.Add("title", expandToolTip);
            image.Attributes.Add("alt", expandToolTip);
            image.ImageUrl = "/_layouts/images/TPMax2.gif";
            control.Style.Add(HtmlTextWriterStyle.Display, "none");
        }

        protected override void OnPreRender(EventArgs e)
        {

            base.OnPreRender(e);
        }
    }

    public class EditorPartTable : Table
    {
        public EditorPartTable()
            : base()
        {
            this.Width = Unit.Percentage(100.0);
            this.CellPadding = 0;
            this.CellSpacing = 0;
            this.BorderWidth = 0;
        }
    }

    public class EditorPartSection : TableRow
    {
    }

    public class EditorPartControlCell : TableCell
    {
        public Label label;
        public Panel header;
        public Panel body;
        public Panel group;

        public EditorPartControlCell(Control control, string labelText, string toolTipText)
            : base()
        {
            if (string.IsNullOrEmpty(toolTipText)) throw new ArgumentNullException("toolTipText");
            if (string.IsNullOrEmpty(labelText)) throw new ArgumentNullException("labelText");

            header = new Panel();
            this.Controls.Add(header);
            header.CssClass = "UserSectionHead";

            label = new Label();
            header.Controls.Add(label);
            label.Text = labelText;
            label.AssociatedControlID = control.ID;
            if (!string.IsNullOrEmpty(toolTipText))
            {
                label.ToolTip = toolTipText;
            }

            body = new Panel();
            body.CssClass = "UserSectionBody";
            this.Controls.Add(body);


            group = new Panel();
            body.Controls.Add(group);
            group.CssClass = "UserControlGroup";
            group.Controls.Add(control);

            Panel panelLine = new Panel();
            this.Controls.Add(panelLine);
            panelLine.CssClass = "UserDottedLine";
        }
    }

    public class EditorPartBooleanSection : TableRow
    {
        public CheckBox checkbox;

        public TableCell cell;

        public EditorPartBooleanSection(string id, string name, string description)
            : base()
        {
            if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id");
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

            checkbox = new CheckBox();
            checkbox.ID = id;
            checkbox.CssClass = "UserInput";
            checkbox.Width = Unit.Percentage(100.0);

            cell = new EditorPartControlCell(checkbox, name, description);
            this.Cells.Add(cell);
        }
    }

    public class EditorPartTextboxSection : TableRow
    {
        public TextBox textbox;

        public TableCell cell;

        public EditorPartTextboxSection(string id, string name, string description)
            : base()
        {
            if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id");
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

            textbox = new TextBox();
            textbox.ID = id;
            textbox.CssClass = "UserInput";
            textbox.Width = Unit.Percentage(100.0);

            cell = new EditorPartControlCell(textbox, name, description);
            this.Cells.Add(cell);
        }
    }

    public class EditorPartAssetUrlSelectorSection : TableRow
    {
        public TextBox textbox;
        public Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector selector;

        public TableCell cell;

        public EditorPartAssetUrlSelectorSection(string id, string name, string description)
            : base()
        {
            if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id");
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

            PlaceHolder ph = new PlaceHolder();

            cell = new EditorPartControlCell(ph, name, description);
            this.Cells.Add(cell);

            textbox = new TextBox();
            ph.Controls.Add(textbox);

            selector = new Microsoft.SharePoint.Publishing.WebControls.AssetUrlSelector();
            selector.ID = id;
            selector.AssetUrlTextBoxVisible = false;
            ph.Controls.Add(selector);
        }

        protected override void OnPreRender(EventArgs e)
        {
            selector.AssetUrlClientID = textbox.ClientID;
            base.OnPreRender(e);
        }
    }
                



    public class EditorPartTextboxWithBuilderSection : TableRow
    {
        public TextBox textbox;

        public TableCell cell;

        public HtmlInputButton button;

        public EditorPartTextboxWithBuilderSection(string id, string name, string description)
            : base()
        {
            if (string.IsNullOrEmpty(id)) throw new ArgumentNullException("id");
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

            textbox = new TextBox();
            textbox.ID = id + "_EDITOR";

            cell = new EditorPartControlCell(textbox, name, description);
            this.Cells.Add(cell);

            // Add Builder button
            button = new HtmlInputButton();
            button.ID = textbox.ID + "_builder";
            //SS: Sorry.. Quite hackish, but I don't feel like refactoring all this.
            textbox.Parent.Controls.AddAt(textbox.Parent.Controls.IndexOf(textbox) + 1, button);

            textbox.CssClass = "UserInput";
            textbox.Width = Unit.Pixel(176);

            button.Value = "...";
            button.Attributes.Add("title", "Click to use builder");
            button.Attributes.Add("class", "ms-PropGridBuilderButton");
            button.Attributes.Add("onfocusout", "this.style.display='none';");
        }

        protected override void OnPreRender(EventArgs e)
        {
            textbox.Attributes.Add("onfocusin", string.Format("MSOPGrid_BuilderVisible({0})", button.ClientID));
            textbox.Attributes.Add("ondeactivate", string.Format("MSOTlPn_prevBuilder={0};", button.ClientID));

            button.Attributes.Add("onclick", string.Format("javascript:MSOPGrid_doBuilder('\u002f_layouts\u002fzoombldr.aspx?culture=en-US', {0}, 'dialogHeight:340px;dialogWidth:430px;help:no;status:no;resizable:yes')", textbox.ClientID));
            base.OnPreRender(e);
        }

    }
}
